﻿
Imports osi.root.utils
Imports osi.root.delegates
Imports osi.root.formation

Public Class filtered_value(Of T)
    Private ReadOnly m As map(Of String, vector(Of pair(Of T, filter_set)))

    Friend Sub New(ByVal fs As filter_selector, ByVal m As filtered_raw_value(Of T))
        assert(Not fs Is Nothing)
        assert(Not m Is Nothing)
        Me.m = New map(Of String, vector(Of pair(Of T, filter_set)))()
        m.foreach(Sub(ByRef key As String,
                      ByRef value As T,
                      ByRef vf As vector(Of pair(Of String, String)))
                      Me.m(key).push_back(make_pair(value, New filter_set(fs, vf)))
                  End Sub)
    End Sub

    Default Public ReadOnly Property v(ByVal key As String,
                                       Optional ByVal variants As vector(Of pair(Of String, String)) = Nothing) As T
        Get
            Dim r As T = Nothing
            assert([get](key, r, variants))
            Return r
        End Get
    End Property

    Public Function [get](ByVal key As String,
                          ByRef v As T,
                          Optional ByVal variants As vector(Of pair(Of String, String)) = Nothing) As Boolean
        Dim it As map(Of String, vector(Of pair(Of T, filter_set))).iterator = Nothing
        it = m.find(key)
        If it = m.end() Then
            Return False
        Else
            Dim vs As vector(Of pair(Of T, filter_set)) = Nothing
            vs = (+it).second
            For i As Int32 = 0 To vs.size() - 1
                If vs(i).second.match(variants) Then
                    v = vs(i).first
                    Return True
                End If
            Next
            Return False
        End If
    End Function
End Class
